//app.js
var q = require('./utils/q.js');
var timer = '';
App({

  // getLocation() {
  //   let self = this;
  //   wx.getLocation({
  //     type: 'gps',
  //     success: function (res) {
  //       self.globalData.location = {};
  //       self.globalData.location.lat = res.latitude;
  //       self.globalData.location.lon = res.longitude;
  //     },
  //     complete: function () {

  //     }
  //   })
  // },

  // 获取系统信息
  getSystemInfo() {
    let self = this
    wx.getSystemInfo({
      success: function (systeminfo) {
        self.globalData.systemInfo = systeminfo;
      }
    });
  },

  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      wx.login({
        success: function (wxlogin) {
          that.globalData.wxcode = wxlogin.code
          wx.getUserInfo({
            success: function (res) {
              let params = Object.assign({}, { code: that.globalData.wxcode }, res.userInfo, that.globalData.systemInfo, that.globalData.location);
              // 获取微信open_id
              that.query('get_open_id', { code: wxlogin.code }).then(res => {
                that.globalData.open_id = res.open_id;
                params.provider = 'wechat_small';
                params.open_id = that.globalData.open_id;
                // 微信登录  获取用户信息
                that.query('wechat', params, 'post').then(res => {
                  if (res.code == 200) {
                    wx.setStorageSync('37yu2_token', res.token);
                    wx.setStorageSync('37yu2_user', res.user);
                    that.globalData.userInfo = res.user;
                    // if (res.user.role == 1) {
                    //   wx.redirectTo({ url: '/pages/tutor/tutor' });
                    // }
                  }
                });
              });

              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      });
    }
  },

  showToast(msg, icon, time, image) {
    wx.hideToast()
    wx.showToast({
      title: msg || '正在请求',
      icon: icon || 'loading',
      image:image || false,
      duration: time || 3000
    });
  },
  showModel(args = {}) {
    return q.Promise((resolve, reject) => {
      wx.showModal({
        title: args.title || '成功',
        content: args.msg,
        showCancel: true,
        confirmText: '确定',
        success: function (res) {
          if (res.confirm) {
            resolve(true)
          } else {
            resolve(false)
          }
        }
      })
    });
  },

  query(url, data = {}, method = 'GET', bool = false) {
    let self = this
    let apiUrl = 'https://api.salesgj.com' + url;
    // let apiUrl = 'http://172.16.0.70:3000' + url
    let location = self.globalData.location;
    if (!!location) {
      data = Object.assign({}, data, location);
    }

    if (!!timer) { clearTimeout(timer); }
    timer = setTimeout(() => {
      self.showToast();
    }, 1000);

    return q.Promise((resolve, reject) => {
      wx.request({
        url: apiUrl,
        data: data,
        method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          Authorization: wx.getStorageSync('sale_token')
        }, // 设置请求的 header
        success: function (res) {
          // if (res.status === 'success') {
            resolve(res.data);
          // } else {
          //   self.showModel( res.msg )
          // }
        },
        fail: function (err) {
          self.showModel({title:'连接失败' ,msg:'网络连接失败'})
          // self.showToast('连接失败', 'success', 2000,'../../assets/warning.png')
          reject(err)
        },
        complete: function (res) {
          // complete
          wx.stopPullDownRefresh();
          clearTimeout(timer);
          wx.hideToast();
        }
      })
    })
  },

  globalData: {
    wxcode: null,
    systemInfo: null,
    userInfo: null,
    location: null,
    limit: 10
  }
})
